package org.easy.alioss;


import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({AliOssProperties.class})
@AutoConfigureAfter
@Slf4j
public class AliOssAutoConfiguration {
    private final AliOssProperties aliOssProperties;

    public AliOssAutoConfiguration(AliOssProperties aliOssProperties) {
        this.aliOssProperties = aliOssProperties;
        checkParam();
    }

    @Bean
    @ConditionalOnMissingBean
    public AliOssTemplate aliOssTemplate() {
        AliOssAdapter ossClientAdapter = new AliOssAdapter();
        return new AliOssTemplate(aliOssProperties, ossClientAdapter);
    }

    @Bean
    @ConditionalOnMissingBean
    public AliOssAdapter aliOssAdapter() {
        return new AliOssAdapter();
    }


    private void checkParam() {
        if (aliOssProperties.getAccessKeyId() == null ||
                aliOssProperties.getAccessKeySecret() == null ||
                aliOssProperties.getProtocol() == null
        ) {
            log.error("未配置" + AliOssProperties.class.getTypeName());
        }

    }


}
